<template>
	<view class="authWrap">
	 <button v-for="(item,index) in roleList" :key="index" @click="authRoleDirect(item.title)">{{item.name}}</button>
	</view>
</template>

<script setup>
import {getSmsCode,testPhoneLogin} from '@/api/index.js'
import {setToken,setClientid,setSaveRole,getToken,getClientid} from '@/utils/auth.js'
import {onLoad} from '@dcloudio/uni-app'
const roleList = [
	{name:"检察院",title:"Procuratorate"},
	{name:"司法局",title:"BureauOfJustice"},
	{name:"行政机关",title:"AdministrativeAgencies"}
]
// 获取短信验证码
const getSmsCodeData = (phoneNum=`15888888886`)=>{
return  new Promise(async(resolve,reject)=>{
	try{
		const {code} = await getSmsCode(phoneNum)
		if(code === 200){
		 const {code:codeInt,data:res} = await testPhoneLogin(phoneNum)
		 if(code === 200){
			 const {token,clientid} = res
			 setToken(token)
			 setClientid(clientid)
			 resolve()
		 }
		}
	 }catch(e){
		//TODO handle the exception
		reject()
		return Promise.reject(e)
	}
})
}

onLoad(()=>{
	// const tokenStorage = getToken()
	// const getClientids = getClientid()
	// if(!tokenStorage  || !getClientids){
	// }
})
const getRole = ()=>{
return new Promise((resolve,reject)=>{
		resolve({
			code:200,
			msg:"请求成功！",
			data:{
				role:"Procuratorate"
			}
		})
	})
}
const authRoleDirect = (rolement)=>{
		setSaveRole(rolement)
	switch(rolement){
		case "Procuratorate": //检察院
		getSmsCodeData().then(()=>{
			uni.switchTab({
				url:"/pages/index/index"
			})
		})
		break;
		case "BureauOfJustice": //司法局（完成）
		getSmsCodeData().then(()=>{
			uni.redirectTo({
				url:"/pages/bureauOfJustice/bureauOfJustice"
			})
		})
		break;
		case "AdministrativeAgencies": //行政机关
		getSmsCodeData('12345678523').then(()=>{
			uni.redirectTo({
				url:"/pages/administrativeAgencies/administrativeAgencies"
			})
		})
		break;
	}
}

</script>

<style lang="scss">
.authWrap{
	height: 100vh;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	button{
		margin-bottom: 30rpx;
	}
}
</style>
